﻿/*
 * Dialog class code provided by Dave Relyea
 * Original information and background can be found
 * here: http://blogs.msdn.com/devdave/archive/2008/06/08/using-popup-to-create-a-dialog-class.aspx
 * */
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Media;
using System.Windows.Input;

namespace InteractChartLib.Controls
{

    public abstract class Dialog : Control
    {
        private Point _location;
        private bool _isShowing;
        private Popup _popup;
        private Grid _grid;
        private Canvas _canvas;
        private FrameworkElement _content;

        public Dialog()
        {
        }

        public void Show(Point location)
        {
            if (_isShowing)
                throw new InvalidOperationException();

            _isShowing = true;
            _location = location;
            EnsurePopup();
            _popup.IsOpen = true;
        }

        public void Close()
        {
            _isShowing = false;

            if (_popup != null)
            {
                _popup.IsOpen = false;
            }
        }

        protected abstract FrameworkElement GetContent();

        protected virtual void OnClickOutside() { }

        private void EnsurePopup()
        {
            if (_popup != null)
                return;

            _popup = new Popup();
            _grid = new Grid();

            _popup.Child = _grid;

            _canvas = new Canvas();

            _canvas.MouseLeftButtonDown += (sender, args) => { OnClickOutside(); };
            _canvas.MouseRightButtonDown += (sender, args) => { args.Handled = true; OnClickOutside(); };

            _canvas.Background = new SolidColorBrush(Colors.Transparent);

            _grid.Children.Add(_canvas);

            _content = GetContent();

            _content.HorizontalAlignment = HorizontalAlignment.Left;
            _content.VerticalAlignment = VerticalAlignment.Top;
            _content.Margin = new Thickness(_location.X, _location.Y, 0, 0);


            _grid.Children.Add(_content);

            UpdateSize();
        }

        private void OnPluginSizeChanged(object sender, EventArgs e)
        {
            UpdateSize();
        }

        private void UpdateSize()
        {
            _grid.Width = Application.Current.Host.Content.ActualWidth;
            _grid.Height = Application.Current.Host.Content.ActualHeight;

            if (_canvas != null)
            {
                _canvas.Width = _grid.Width;
                _canvas.Height = _grid.Height;
            }
        }
    }


    #region [ CHART Contextual menu DIALOG ]
    public class ChartContextMenu : Dialog 
    {
        /// <summary>
        /// Occurs when Drop.
        /// </summary>
        public event RoutedEventHandler NotifyClosed;


        public ChartContextMenu()
        {
        }

        protected override void OnClickOutside()
        {
            Close();
        }

        protected override FrameworkElement GetContent()
        {
            Grid grid = new Grid() { Width = 100, Height = 115 };
            Border border = new Border() { BorderBrush = new SolidColorBrush(Colors.Black), BorderThickness = new Thickness(1), Background = new SolidColorBrush(Colors.LightGray) };
            grid.Children.Add(border);

            TextBlock ZoomPlus = new TextBlock() { Text = "Zoom +", Width = 90 };
            ZoomPlus.MouseLeftButtonUp += new MouseButtonEventHandler(SetZoomPlusModality);

            TextBlock ZoomMinus = new TextBlock() { Text = "Zoom -", Width = 90 };
            ZoomMinus.MouseLeftButtonUp += new MouseButtonEventHandler(SetZoomMinusModality);

            TextBlock ZoomWindow = new TextBlock() { Text = "Zoom Window", Width = 90 };
            ZoomWindow.MouseLeftButtonUp += new MouseButtonEventHandler(SetZoomWindowModality);

            TextBlock NoZoom = new TextBlock() { Text = "No Zoom", Width = 90 };
            NoZoom.MouseLeftButtonUp += new MouseButtonEventHandler(SetNoZoomModality);


            
            ListBox options = new ListBox();
            options.Items.Add(ZoomPlus);
            options.Items.Add(ZoomMinus);
            options.Items.Add(ZoomWindow);
            options.Items.Add(NoZoom);

            grid.Children.Add(options);

            return grid;
        }

        void CloseMenu(object sender, MouseButtonEventArgs e)
        {
            if (NotifyClosed != null)
            {
                NotifyClosed(sender, e);
            }
            Close();
        }

        void CancelContextMenu(object sender, MouseButtonEventArgs e)
        {
            CloseMenu(sender, e);
        }

        void SetZoomPlusModality(object sender, MouseButtonEventArgs e)
        {
            e.Handled = true;

            CloseMenu(ChartContextMenuActions.ZoomPlus, e);
        }
        void SetZoomMinusModality(object sender, MouseButtonEventArgs e)
        {
            e.Handled = true;

            CloseMenu(ChartContextMenuActions.ZoomMinus, e);
        }
        void SetZoomWindowModality(object sender, MouseButtonEventArgs e)
        {

            e.Handled = true;
            CloseMenu(ChartContextMenuActions.ZoomWindow, e);
        }
        void SetNoZoomModality(object sender, MouseButtonEventArgs e)
        {

            e.Handled = true;
            CloseMenu(ChartContextMenuActions.NoZoom, e);
        }

    }

    public enum ChartContextMenuActions
    {
        ZoomPlus,
        ZoomMinus,
        ZoomWindow,
        NoZoom
    };

    #endregion
}
